// components/ng-popup/index.js
const app = getApp()
Component({
  properties: {
    show: {
      type: Boolean,
      value: false,
    },
    contentSlot: {
      type: Boolean,
      value: false
    },
    value: {
      type: Object,
      value: () => ({})
    },
    title: {
      type: String,
      value: app.data.$language.l205
    },
    placeholder: {
      type: String,
      value: app.data.$language.l206
    },
    rule: {
      type: Boolean,
      value: false
    }
  },
  observers: {
    value(val) {
      let body = {}
      if (val.id) {
        body.inputDefaultValue = val.name
        body.id = val.id
        body.inputValue = val.name
      } else {
        body.inputDefaultValue =''
        body.id = ''
        body.inputValue = ''
      }
      this.setData(body)
    }
  },
  data: {
    inputValue: '',
    id: '',
    inputDefaultValue: '',
    isFoucs: false,
    l208:app.data.$language.l208,
    l44:app.data.$language.l44,
  },
  methods: {
    onInputChangeHandle({detail: {value}}) {
      this.data.inputValue = value
    },
    onConfirm() {
      if (this.data.inputValue) {
        let value = { name: this.data.inputValue }
        if (this.data.id) {
          value.id = this.data.id
        }
        this.triggerEvent('confirm', value)
      } else {
        wx.showToast({
          title:app.data.$language.l207,
          icon: 'none',
          duration: 2000,
        })
      }
    },
    onCancel() {
      this.onClose()
    },
    onClose() {
      this.triggerEvent('close')
    },
    onFoucus() {
      this.setData({ isFoucs: true })
    },
    onBlur() {
      this.setData({ isFoucs: false })
    }
  }
})
